package com.spring.camel.project;

import lombok.extern.slf4j.Slf4j;
import org.apache.camel.*;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultMessage;

@Slf4j
public class Camel2 {
    public static void main(String[] args) throws Exception {
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:start")
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                Message message = exchange.getMessage();
                                message.setHeader("token","haha");
                                log.info("---------------set message-------------------");

                                Object body = exchange.getIn().getBody();
                                exchange.getIn().setBody(body + "--- new Message");
                            }
                        })
                        .to("seda:end");
            }
        });

        camelContext.start();
        ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
        producerTemplate.sendBody("direct:start","hello everybody");

        ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
        String s = consumerTemplate.receiveBody("seda:end", String.class);
        log.info("获得的消息：{}",s);
    }
}
